package com.aidex.web.controller.risk;

import com.aidex.common.core.page.TableDataInfo;
import com.aidex.common.utils.JsonHelper;
import com.aidex.common.utils.http.HttpUtils;
import com.aidex.config.InitMq;
import com.aidex.quartz.task.SendTask;
import com.aidex.risk.domain.*;
import com.aidex.system.domain.Waybill;
import com.aidex.system.domain.WaybillDetail;
import com.aidex.system.service.WaybillDetailService;
import com.aidex.system.service.WaybillService;
import com.aidex.system.service.impl.GoodsRiskInfo;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.ibm.mq.jms.MQQueueConnectionFactory;
import com.ibm.msg.client.wmq.WMQConstants;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import javax.jms.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.Queue;

@RestController
@RequestMapping("/risk/out/goodsrisk")
public class GoodsRiskController {

    private static final Logger log = LoggerFactory.getLogger(InitMq.class);

    private static MessageProducer producer;
    private static TextMessage message;
    @Autowired
    private WaybillService waybillService;
    @Autowired
    private WaybillDetailService detailService;
    @PostMapping("sendMqApi")
    public String sendMqApi(HttpServletRequest request, HttpServletResponse response) throws JMSException {
        String service = "";
        try {
            StringBuffer sb = new StringBuffer();
            String line="";
            BufferedReader reader = request.getReader();
            while ((line = reader.readLine()) != null){
                sb.append(line);
            }

            service = sb.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        String res =SendMQ(service);
        return res;
    }

    @PostConstruct
    public void init() throws JMSException {
        MQQueueConnectionFactory mqQueueConnectionFactory = new MQQueueConnectionFactory();
        Connection connection = null;
        try {
            mqQueueConnectionFactory.setHostName("10.80.33.208");//总线MQ统一入口服务器IP
            mqQueueConnectionFactory.setTransportType(WMQConstants.WMQ_CM_CLIENT);
            mqQueueConnectionFactory.setCCSID(1208);
            mqQueueConnectionFactory.setChannel("ESB.ADMIN.SVRCONN");//总线MQ统一入口通道
            mqQueueConnectionFactory.setPort(40001);//总线MQ统一入口端口
            mqQueueConnectionFactory.setQueueManager("ESB_QM");//总线MQ统一入口队列管理器

            connection = mqQueueConnectionFactory.createConnection();;
            Session session = connection.createSession(false,
                    Session.AUTO_ACKNOWLEDGE);
            javax.jms.Queue queue = session.createQueue("flfj_txd_to_ersuo_queue");//总线MQ统一入口队列名称
            producer = session.createProducer(queue);
            connection.start();
            message = session.createTextMessage();
        } catch (Exception e) {
            e.printStackTrace();

        }

    }

    String SendMQ(String  json) {
        try {
            log.info("test debug SendMQ发送数据："+json);
            message.setText(json);
            producer.send(message);
            return "OK";
        } catch (Exception e) {
            e.printStackTrace();
            return e.getMessage();
        }
    }

}

